package org.xin.framework.base.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONString;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.xin.common.result.Result;
import org.xin.framework.base.pojo.form.SearchForm;
import org.xin.framework.base.service.IBaseService;

import java.io.Serializable;
import java.util.List;

/**
 * @author ：xin
 * @date ：Created in 18:19
 * @description：
 * @modified By：$
 * @version: $
 */
public class IBaseController<DOMAIN, S extends IBaseService<DOMAIN>> {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 动态 IOC代理
     *
     * 多个需要使用Autowired
     */
    @Autowired
    protected S service;

    /*@ApiOperation("Super 分页查询(支持高级搜索)")
    @PostMapping({"superPage", "superPage/{limit}", "superPage/{page}/{limit}"})
    public Result superPage(
            @PathVariable(value = "page", required = false)
            @ApiParam(value = "页", example = "0", defaultValue = "0", type = "int") Integer page,
            @PathVariable(value = "limit", required = false)
            @ApiParam(value = "条", example = "10", defaultValue = "10", type = "int") Integer limit,
            @RequestBody List<SearchForm> forms
            ){
        return Result.success(service.pageForms(page, limit, forms));
    }*/

    @ApiOperation("Super 高级搜索")
    @PostMapping("superSearch")
    public Result superSearch(@RequestBody JSONObject object){
        return Result.success(service.search(object, service.getDomainClass()));
    }

    @ApiOperation("Super 子列表(仅包含父子关系模块可用)")
    @GetMapping("superChildren/{parentId}")
    public Result superChildren(@PathVariable("parentId") String parentId){
        return Result.success(service.children(parentId));
    }

    /*@ApiOperation("Super 根据ID删除")
    @DeleteMapping("superDelete/{id}")
    public Result superDelete(@PathVariable("id") String id){
        return Result.success(service.removeById(id));
    }*/

    @ApiOperation("Super 根据ID删除(批量)")
    @DeleteMapping("superDelete")
    public Result superDelete(@RequestBody List<String> ids){
        return Result.success(service.removeByIds(ids));
    }

    /*@ApiOperation("Super 全部列表")
    @GetMapping("superList")
    public Result superList(){
        return Result.success(service.list());
    }*/

    @ApiOperation("Super 添加/编辑")
    @PostMapping("superEdit")
    public Result edit(DOMAIN domain){
        return Result.check(service.saveOrUpdate(domain));
    }
}
